<template>
  <view>
    <!-- 用户信息开始 -->
    <view class="user_info">
      <view class="user_icon">
        <image :src="imageDetail.user.avatar" mode="widthFix" />
      </view>
      <view class="user_desc">
        <view class="user_name" v-text="imageDetail.user.name"></view>
        <view class="user_time" v-text="imageDetail.cnTime"></view>
      </view>
    </view>
    <!-- 用户信息结束 -->

    <!-- 高清大图开始 -->
    <view class="high_iamge">
      <swiper-action @swiperAction="handleSwiperAction">
        <image :src="imageDetail.thumb" mode="widthFix" />
      </swiper-action>
    </view>
    <!-- 高清大图结束 -->

    <!-- 点赞开始 -->
    <view class="user_rank">
      <view class="rank">
        <text class="iconfont icondianzan" v-text="imageDetail.rank"></text>
      </view>
      <view class="user_collect">
        <text class="iconfont iconshoucang">收藏</text>
      </view>
    </view>
    <!-- 点赞结束 -->
    <!-- 专辑开始 -->
    <veiw class="album_wrap">
      <view class="album_title">相关</view>
      <view class="album_comtent">
        <view class="album_item" v-for="item in album" :key="item.id">
          <view class="album_cover">
            <image :src="item.cover" mode="aspectFill" />
          </view>
          <view class="album_info">
            <view class="album_info_text">专辑</view>
            <view class="album_info_name" v-text="item.name"></view>
            <view class="iconfont iconiconfontjiantou4"></view>
          </view>
        </view>
      </view>
    </veiw>
    <!-- 专辑结束 -->

    <!-- 评论模块开始 -->
    <view class="comment hot">
      <!-- 最热评论 -->
      <view class="comment_title">
        <text class="iconfont iconhot1"></text>
        <text class="comment_text">最热评论</text>
      </view>
      <view class="comment_list">
        <view class="comment_item" v-for="item in hot" :key="item.id">
          <!-- 用户信息 -->
          <view class="comment_user_info">
            <view class="user_icon">
              <image :src="item.user.avatar" mode="widthFix" />
            </view>
            <view class="user_div">
              <view class="user_name" v-text="item.user.name"></view>
              <view class="user_time" v-text="itme.atime"></view>
            </view>
            <!-- 用户徽章 -->
            <view class="user_badge">
              <image
                :src="badge.icon"
                mode=""
                v-for="(badge, index) in item.title"
                :key="index"
              />
            </view>
          </view>
          <!-- 评论数据 -->
          <view class="comment_desc">
            <view class="comment_content" v-text="item.content"></view>
            <view class="comment_like">
              <text class="iconfont icondianzan" v-text="item.size"></text>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 评论模块结束 -->

    <!-- 下载图片开始 -->
    <view class="download">
      <view class="download_btn" @click="handleDownload">
        下载图片
      </view>
    </view>
    <!-- 下载图片结束 -->
  </view>
</template>

<script>
import moment from "moment";
import swiperAction from "@/components/swiperAction";

// 设置语言为中文
moment.locale("zh-cn");
export default {
  data() {
    return {
      // 图片信息对象，包含用户头像
      imageDetail: {},
      // 专辑数据 数组
      album: [],
      // 最热评论
      hot: [],
      // 最新评论
      comment: [],
      // 图片索引
      imageIndex: 0,
    };
  },
  methods: {
    /* 下载图片 */
    async handleDownload(){
      await uni.showToast({
        title: '下载中',
        duration: 2000
      });
      // console.log("下载图片");
      // 1、将远程图片文件下载到小程序的内存中(tempFilePath：图片临时路径)
      const result1 = await uni.downloadFile({url: this.imageDetail.img});
      const {tempFilePath} = result1[1];

      // console.log("debug => ",tempFilePath);

      // 2、将小程序内存中的文件保存到本地
      const result2 = await uni.saveImageToPhotosAlbum({filePath: tempFilePaths});
      // 3、提示用户下载成功
      uni.hideLoading();
      await uni.showToast({
        title: '下载成功',
        duration: 2000
      });
    },
    /* 给当前页面赋值 */
    getData() {
      const { imageList } = getApp().globalData;
      this.imageDetail = imageList[this.imageIndex];
      // this.imageDetail.newthumb = this.imageDetail.thumb + this.imageDetail.rule.replace("$<Height>", 360);

      //处理时间戳(显示 2 年前)
      this.imageDetail.cnTime = moment(this.imageDetail.atime * 1000).fromNow();

      this.getComments(this.imageDetail.id);
    },
    /* 监听滑动事件 */
    handleSwiperAction(event) {
      console.log(event);
      const { imageList } = getApp().globalData;
      // 规定左滑 imageIndex++
      if (event.direction === "left" && this.imageIndex < imageList.length - 1) {
        // 可以进行左滑(加载下一页)
        this.imageIndex++;
        this.getData();
      } else if (event.direction === "right" && this.imageIndex > 0) {
        // 可以进行右滑(返回上一页)
        this.imageIndex--;
        this.getData();
      } else{
        uni.showToast({
          title: '没有数据了哦',
          duration: 2000,
          icon: "none"
        });
      }
    },
    /* 获取接口数据 */
    getComments(id) {
      this.request({
        url: `http://157.122.54.189:9088/image/v2/wallpaper/wallpaper/${id}/comment`,
      }).then((result) => {
        console.log(result);
        this.album = result.res.album;
        this.hot = result.res.hot;
        this.comment = result.res.comment;
      });
    },
  },
  /* 页面生命周期函数，监听页面加载 */
  onLoad() {
    console.log(getApp().globalData);
    const { imageList, imageIndex } = getApp().globalData; // const修饰，不能再被改变
    this.imageIndex = imageIndex;
    this.getData();
  },
  /* 注册引入的组件 */
  components: {
    swiperAction,
  },
};
</script>

<style lang="scss">
.user_info {
  display: flex;
  padding: 20rpx;
  .user_icon {
    padding: 0 20rpx;
    image {
      width: 88rpx;
      border-radius: 50%;
    }
  }

  .user_desc {
    .user_name {
      color: #000;
      font-weight: 600;
    }
    .user_time {
      color: #ccc;
      font-size: 24rpx;
      padding: 10rpx 0;
    }
  }
}

.user_rank {
  display: flex;
  height: 80rpx;
  border-bottom: 5rpx solid #eee;
  .rank {
    display: flex;
    flex: 1;
    justify-content: center; // 水平居中
    align-items: center; // 垂直居中
    .iconfont {
    }
  }

  .user_collect {
    display: flex;
    flex: 1;
    justify-content: center; // 水平居中
    align-items: center; // 垂直居中
    .iconfont {
    }
  }
}
// 专辑样式
.album_wrap {
  padding: 20rpx;
  .album_title {
    padding-left: 10rpx;
    padding: 10rpx 0;
  }

  .album_comtent {
    .album_item {
      display: flex;
      padding: 10rpx 0;
      border-bottom: 10px solid #eee;
      .album_cover {
        flex: 1;
        image {
          width: 180rpx;
          height: 180rpx;
        }
      }

      .album_info {
        flex: 3;
        padding-left: 20rpx;
        .album_info_text {
          width: 100rpx;
          height: 50rpx;
          background-color: $color-text;
          color: #fff;
          display: flex;
          justify-content: center; // 水平居中
          align-items: center; // 垂直居中
        }

        .album_info_name {
          padding: 10rpx 0;
          color: #888;
        }
        .iconfont {
          font-size: 40rpx;
          position: absolute; // 根据父级元素定位
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }
  }
}

// 最热评论
.comment {
  .comment_title {
    padding: 15rpx;
    .iconfont {
      color: red;
      font-size: 40rpx;
    }

    .comment_text {
      padding-left: 10rpx;
      font-weight: 600; // 加粗
      font-size: 30rpx;
      color: #666;
    }
  }

  // 最新评论
  .comment_list {
    .comment_item {
      // 用户信息
      border-bottom: 15px solid #eee;
      .comment_user_info {
        display: flex; // 伸缩
        .user_icon {
          width: 15%;
          display: flex;
          justify-content: center; // 水平居中
          align-items: center; // 垂直居中
          image {
            width: 90%;
          }
        }

        .user_div {
          flex: 1;
          .user_name {
            color: #777;
          }

          .user_time {
            color: #ccc;
            font-size: 24rpx;
            padding: 5rpx;
          }
        }

        .user_badge {
          image {
            width: 40rpx;
            height: 40rpx;
            display: inline-block; // 行内块
          }
        }
      }
      // 评论数据
      .comment_desc {
        display: flex;
        padding: 30rpx 0;
        .comment_content {
          flex: 1;
          padding-left: 15%;
          color: #000;
        }

        .comment_like {
          text-align: right; // 右对齐
          .iconfont {
          }
        }
      }
    }
  }
}

// 下载图片
.download {
  height: 120rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  .download_btn {
    width: 95%;
    height: 85%;
    background-color: $color-text;
    color: #fff;
    font-size: 40rpx;
    font-weight: 600;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
